//
//  ERefreshHeader.m
//  Common
//
//  Created by guoshiqing on 2018/6/15.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "ERefreshHeader.h"
#import "DDClientState.h"

@interface ERefreshHeader ()

@property (weak, nonatomic) UIImageView *logoLoading;

@property (weak, nonatomic) UIView *resltView;
@property (weak, nonatomic) UILabel *resltLabel;
@property (weak, nonatomic) UIImageView *resltImg;
@end

@implementation ERefreshHeader

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare {
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 50;
    
    //添加 logoLoading
    UIImageView *logoImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"refresh_load"]];
    logoImg.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:logoImg];
    self.logoLoading = logoImg;
    
    //添加 resltView
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    [self addSubview:view];
    self.resltView = view;
    
    // 添加resltLabel
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor colorWithRed:114/255.0 green:114/255.0 blue:114/255.0 alpha:1.0f];
    label.font = [UIFont boldSystemFontOfSize:14];
    [self.resltView addSubview:label];
    self.resltLabel = label;
    
    // resltImg
    UIImageView *resltImg = [[UIImageView alloc] init];
    resltImg.contentMode = UIViewContentModeScaleAspectFit;
    [self.resltView addSubview:resltImg];
    self.resltImg = resltImg;
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews {
    [super placeSubviews];
    
    self.logoLoading.center = CGPointMake(self.mj_w * 0.5, self.mj_h * 0.5);
    
    self.resltView.frame = self.bounds;
    
    self.resltImg.bounds = CGRectMake(0, 0, 16, 16);
    self.resltImg.center = CGPointMake(self.resltView.mj_w * 0.5 - 40, self.resltView.mj_h  * 0.5);
    
    self.resltLabel.bounds = CGRectMake(0, 0, 90, 25);
    self.resltLabel.center = CGPointMake(self.resltView.mj_w * 0.5 + 23, self.resltView.mj_h  * 0.5);
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change {
    [super scrollViewContentOffsetDidChange:change];
    if (self.state == MJRefreshStateRefreshing) { //在转圈圈的时候，上滑取消
        if (self.scrollView.contentOffset.y > - self.mj_h) {
            [self endRefreshing];
        }
    }
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change {
    [super scrollViewContentSizeDidChange:change];
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change {
    [super scrollViewPanStateDidChange:change];
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state {
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
            [self logoLoadingStopAnimating];
            self.resltView.hidden = YES;
            break;
        case MJRefreshStatePulling:
            [self logoLoadingStopAnimating];
            self.resltView.hidden = YES;
            break;
        case MJRefreshStateRefreshing:
            self.resltView.hidden = YES;
            [self logoLoadingStartAnimating];
            break;
        default:
            break;
    }
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent {
    [super setPullingPercent:pullingPercent];
    [self logoLoadingScaleRotatAnimation:pullingPercent];
}

//开始无穷旋转
- (void)logoLoadingStartAnimating {
    self.logoLoading.hidden = NO;
    [self.logoLoading setTransform:CGAffineTransformMakeScale(1.0f, 1.0f)];
    
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = @(M_PI * 2.0);
    rotationAnimation.duration = 0.6f;
    rotationAnimation.autoreverses = NO;
    rotationAnimation.repeatCount = HUGE_VALF;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.logoLoading.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    [self.logoLoading sizeToFit];
}

//停止旋转
- (void)logoLoadingStopAnimating {
    self.logoLoading.hidden = YES;
    [self.logoLoading.layer removeAnimationForKey:@"rotationAnimation"];
}

//旋转,放大动画
- (void)logoLoadingScaleRotatAnimation:(CGFloat)pullingPercent {
    //高度：
    float y = (1 - pullingPercent ) * self.mj_h / 2;
    //角度：
    float rotationAngle = M_PI * 2 * pullingPercent;
    
    self.logoLoading.hidden = NO;   //有动画，开始显现
    
    CGAffineTransform t1 = CGAffineTransformMakeTranslation(0, MAX(y, 0));
    CGAffineTransform t2 = CGAffineTransformScale(t1, MIN(pullingPercent, 1.0f), MIN(pullingPercent, 1.0f));
    self.logoLoading.transform = CGAffineTransformRotate(t2, rotationAngle);
}

- (void)endRefreshingWithRetunResultSucceed:(BOOL)isSucceed {
    if (self.state != MJRefreshStateRefreshing) {
        return;
    }
    
    self.resltView.hidden = NO;
    [self logoLoadingStopAnimating];
    if (isSucceed) {
        self.resltImg.image = [UIImage imageNamed:@"refresh_succeed"];
        
        
        if ([DDClientState shareInstance].networkState == NetWorkDisconnect) //无网络
        {
            self.resltImg.image = [UIImage imageNamed:@"refresh_defeat"];
            self.resltLabel.text = @"没有网络哦~";
        }else
        {
            self.resltLabel.text = @"刷新成功";
        }
    } else {
        self.resltImg.image = [UIImage imageNamed:@"refresh_defeat"];
        if ([DDClientState shareInstance].networkState == NetWorkDisconnect) //无网络
        {
            self.resltLabel.text = @"没有网络哦~";
        }else
        {
            self.resltLabel.text = @"刷新失败";
        }
    }
    
    [self performSelector:@selector(endRefreshing) withObject:nil afterDelay:1.0f];
}

@end
